﻿#include "injectormanager.h"
#include <QMessageBox>
#include <QDebug>

#include <QDir>
#include <QTextStream>
#include <QDateTime>


InjectorManager::InjectorManager(SerialDriver* p):
    port(p)
{/*
    for(int i=0;i<4;i++)
    QObject::connect(&m_timer[i], &QTimer::timeout, this, &InjectorManager::handleTimeout);

    m_timer[0].setInterval(3000);
    m_timer[1].setInterval(10000);
    m_timer[0].start();
    m_timer[1].start();
    */
}

void InjectorManager::handlePort()
{
    const QByteArray data = port->m_serial->readAll();
    int idx;
    bool isfirst = readData(data, idx);

    //make send pack
    QByteArray sendpack;
    sendpack.resize(32);
    //receiver
    sendpack[0] = 0x00;//data[2];
    sendpack[1] = 0x03;//data[3];
    sendpack[2] = 0x03;//data[4];

    sendpack[3] = data[0];
    sendpack[4] = data[2];

    //sender
    sendpack[5] = 0x00;//data[0];
    sendpack[6] = 0x02;//data[1];
    sendpack[7] = 0x02;//data[2];
    sendpack[8] = isfirst;

    sendpack[9] = setting[idx].volume;
    sendpack[10] = setting[idx].volume;
    sendpack[11] = setting[idx].period;
    //current max
    sendpack[12] = setting[idx].cur_max;
    //volt min
    sendpack[13] = setting[idx].volt_min >> 8;
    sendpack[14] = setting[idx].volt_min & 0xF;

    sendpack[30] = 0xcc;
    sendpack[31] = 0xcc;

    //send back
    port->writeData(sendpack);

    //update status
    emit updateStatus();

}

void InjectorManager::updateSetting(QList<Setting> list)
{
    int i=0;
    foreach(const Setting& set, list)
    {
        setting[i++] = set;
        //m_timer[i].setInterval(set.period * MSECOND_PER_HOUR);
    }
}

bool InjectorManager::readData(const QByteArray& data, int& idx)
{
    static int i = 0;
    int inj_idx=0;

    // sending port
    int in_ip1  = data.constData()[2];
    int in_ip2  = data.constData()[3];
    int in_port = data.constData()[4];

    //if first
    bool isfirst = data.constData()[5];
    if(isfirst) // assign address
    {
        inj_idx = i;
        i++; i %= 4;
        injector[inj_idx].in_ip = in_ip1 + in_ip2;
        injector[inj_idx].in_port = in_port;
    }
    else
    {
        for(int k=0;k<4;k++)
        {
            if(in_ip1 + in_ip2 == injector[k].in_ip &&
                       in_port == injector[k].in_port)
            {
                inj_idx = k;
                break;
            }
        }
    }

    // setting volume (ml)
    int volh = data.constData()[6];
    int voll = data.constData()[7];
    injector[inj_idx].setting.volume = voll + (volh << 8);

    // setting period (h)
    injector[inj_idx].setting.period = data.constData()[8];

    // if setting changed
    if(isfirst)
    {
        setting[inj_idx] = injector[inj_idx].setting;
        //qDebug() << volh << " "<<voll << " "<<injector[inj_idx].setting.volume <<endl;

        //m_timer[inj_idx].setInterval(setting[inj_idx].period * MSECOND_PER_HOUR);
    }
    else
    {
       //m_timer[inj_idx].stop();
       if(injector[inj_idx].setting != setting[inj_idx])
       {
           //first different, send new setting
           if(!apply_setting[inj_idx])
           {
               apply_setting[inj_idx] = true;
           }
           else
           {
                QMessageBox::warning(NULL, QStringLiteral("警告"), QString::number(inj_idx) + " is offline", QMessageBox::Ok, QMessageBox::Ok);
                //send warning not applied
                apply_setting[inj_idx] = false;
           }
       }

    }
    //m_timer[inj_idx].start();

    // status battery (%)
    injector[inj_idx].status_battery = data.constData()[9] & 0xFF;

    // status volume remain (ml)
    injector[inj_idx].status_volremain = data.constData()[10] & 0xFF;

    // error
    injector[inj_idx].error = data.constData()[13];

    idx = inj_idx;

    emit sigUploadDataInj(inj_idx, injector[inj_idx]);

    return isfirst;
}
/*
void InjectorManager::handleTimeout()
{
    QTimer* ptr = (QTimer*)sender();
    for(int idx = 0;idx<4;idx++)
        if(ptr == &(m_timer[idx]))
        {
            QMessageBox::warning(NULL, "警告", QString::number(idx) + " is offline", QMessageBox::Ok, QMessageBox::Ok);
            break;
        }
}
*/

InjectorManager::~InjectorManager()
{

}

QList<Setting> InjectorManager::getSetting()
{
    return QList<Setting>{setting[0],setting[1],setting[2],setting[3]};
}

